package com.king.designpatterns.hybriddesignpattern.hybriddesignpattern.specificationpattern;

/**
 * like规格书
 *
 * @author king
 * @date 2021-06-16 16:33
 */
public class UserByNameLike extends CompositeSpecification{

    /**
     * like的标记
     */
    private static String LIKE = "%";
    /**
     * 基准的like字符串
     */
    private String likeStr;

    public UserByNameLike(String name) {
        this.likeStr = name;
    }

    /**
     * 实现校验方法
     */
    @Override
    public boolean isSatisfiedBy(User user) {
        boolean result = false;
        String name = user.getName();
        // 去掉%的字符串
        String str = likeStr.replace("%", "");
        if (likeStr.startsWith(LIKE) && likeStr.endsWith(LIKE)){
            result = name.contains(str);
        }else if(likeStr.startsWith(LIKE)){
            result = name.endsWith(str);
        }else{
            result = name.startsWith(str);
        }
        return result;
    }
}
